<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 
// +----------------------------------------------------------------------

namespace app\client\controller;

use service\DataService;
use service\ToolsService;
use controller\BasicClient;
use think\Db;

/**
 * 统计管理
 * Class Sta
 * @package app\client\controller
 * @author  
 * @date 2019/07/24
 */
class Sta extends BasicClient
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'Area';
    public $article = 'Article';
    public $statistics = 'Statistics';
    public $category;
    public $statistic;
    public $areaList;

    public function _initialize()
    {
        parent::_initialize();
        $_areaList = $this->AR->order('area_sort asc, area_id asc')->select();
        $this->areaList = ToolsService::arr2table($_areaList, 'area_id', 'area_parent_id');

        $this->category = model('Category');
        // $this->categorys = $this->category->getLists(['parentid' => 1], 'sort desc', 'id,catname,module');
        $this->categorys1 = $this->category->getLists(['parentid' => 1], 'sort desc', 'id,catname,module');//课件
        $this->categorys2 = $this->category->getLists(['id' => 6], 'sort desc', 'id,catname,module');//新闻动态
    }

    /**
     * 显示地区列表
     */
    public function index() {
        $this->title = '统计管理';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);
            
            //诊疗技术
            $chas = Db::name('Characteristic')->where(['status' => 1,'aid' => ['in', $vo['ids']], 'userid' => session('c_user.id')])->select();
            $vo['cha']['total'] = count($chas);
            $vo['cha']['catname'] = '诊疗技术';
            $vo['cha']['url'] = url('/client').'#/client/characteristic/index?spm=m-288-282';

            //药品
            $drugs = Db::name('Drug')->where(['status' => 1,'aid' => ['in', $vo['ids']], 'userid' => session('c_user.id')])->select();
            $vo['drug']['catname'] = '药品';
            $vo['drug']['total'] = count($drugs);
            $vo['drug']['url'] = url('/client').'#/client/drug/index?spm=m-289-284';

            //新闻动态
            foreach ($this->categorys2 as $k => $v) {
                // halt($vo['ids']);
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $vo['ids']],
                    'userid' => session('c_user.id'),
                ];
                $vo['news'][$v['id']]['catname'] = $v['catname'];
                $vo['news'][$v['id']]['total'] = Db::name($v['module'])->where($map)->count();
                $vo['news'][$v['id']]['url'] = url('/client')."#/client/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-290-230';
            }

            //课件
            foreach ($this->categorys1 as $k => $v) {
                // halt($vo['ids']);
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $vo['ids']],
                    'userid' => session('c_user.id'),
                ];
                $vo['cate'][$v['id']]['catname'] = $v['catname'];
                $vo['cate'][$v['id']]['total'] = Db::name($v['module'])->where($map)->count();
                $vo['cate'][$v['id']]['url'] = url('/client')."#/client/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-290-230';
            }

            // halt($clients);
        }
    }

    public function _ajax_get_admin()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);            

            //诊疗技术
            $chas = Db::name('Characteristic')->where(['status' => 1,'aid' => ['in', $data['ids']], 'userid' => session('c_user.id')])->select();
            $datas[$key]['cha']['total'] = count($chas);
            $datas[$key]['cha']['catname'] = '诊疗技术';
            $datas[$key]['cha']['url'] = url('/client').'#/client/characteristic/index?spm=m-288-282&aid='.$data['area_id'];

            //药品
            $drugs = Db::name('Drug')->where(['status' => 1,'aid' => ['in', $data['ids']], 'userid' => session('c_user.id')])->select();
            $datas[$key]['drug']['catname'] = '药品';
            $datas[$key]['drug']['total'] = count($drugs);
            $datas[$key]['drug']['url'] = url('/client').'#/client/drug/index?spm=m-289-284&aid='.$data['area_id'];

            //新闻动态
            foreach ($this->categorys2 as $k => $v) {
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $data['ids']],
                    'userid' => session('c_user.id'),
                ];
                $datas[$key]['news'][$k]['catname'] = $v['catname'];
                $datas[$key]['news'][$k]['total'] = Db::name($v['module'])->where($map)->count();
                $datas[$key]['news'][$k]['url'] = url('/client')."#/client/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-290-230';
            }

            //课件
            foreach ($this->categorys1 as $k => $v) {
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $data['ids']],
                    'userid' => session('c_user.id'),
                ];
                $datas[$key]['cate'][$k]['catname'] = $v['catname'];
                $datas[$key]['cate'][$k]['total'] = Db::name($v['module'])->where($map)->count();
                $datas[$key]['cate'][$k]['url'] = url('/client')."#/client/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-290-230';
            }

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

    //图表
    public function charts() {
        // $this->title = '图表统计';
        $map['area_parent_id'] = $this->aid;
        $areaData = $this->AR->order('area_sort asc, area_id asc')->where($map)->field('area_id,area_name')->select();
        $diseases = Db::name('Disease')->order('sort asc, id asc')->where(['status' => 1])->field('id,title')->select();
        $nations = Db::name('Nation')->order('sort asc, id asc')->where(['status' => 1])->field('id,title')->select();
        
        $dData = $setData = [];
        $xData1 = array_column($areaData, 'area_name');
        $xData2 = array_column($diseases, 'title');
        $xData3 = array_column($nations, 'title');
        $yData = array_column($this->categorys1, 'catname');
        $setData = $xData1;//数据集
        array_unshift($setData, 'num');
        $setData = [$setData];
        // halt($xData3);
        foreach ($this->categorys1 as $k => $v) {
            $vData = [$v['catname']];
            foreach ($areaData as $kk => $vv) {
                // $vv['ids'] = join(',', ToolsService::getArrSubIds($this->areas, $vv['area_id'], 'area_id', 'area_parent_id'));
                $vv['ids'] = _getAIDs($vv['area_id']);
                $map = [
                    // 'is_deleted' => '0',
                    'status' => 1,
                    'aid' => ['in', $vv['ids']],
                ];
                $proData = Db::name($v['module'])->where($map)->count();
                array_push($dData, [$kk, $k, $proData]);
                array_push($vData, $proData);
            }
            array_push($setData, $vData);
        }
        // halt($setData);
        // $this->assign('dData', json_encode($dData));
        $data = [
            'xData1' => $xData1,//x轴
            'xData2' => $xData2,//x轴
            'xData3' => $xData3,//x轴
            'yData' => $yData,//y轴
            'dData' => $dData,//第三维度数据值
            'setData' => $setData
        ];
        return $this->fetch('', $data);
    }

    /**
     * 显示地区列表
     */
    public function cha() {
        $this->title = '诊疗技术统计';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _cha_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $vo['ids']],
            ];
            
            //诊疗技术
            $chas = Db::name('Characteristic')->where(['status' => 1,'aid' => ['in', $vo['ids']], 'userid' => session('c_user.id')])->select();
            $vo['cha']['total'] = count($chas);
            $vo['cha']['catname'] = '诊疗技术';
            $vo['cha']['url'] = url('/client').'#/client/characteristic/index?spm=m-288-282';

            // halt($clients);
        }
    }

    public function _ajax_get_cha()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $data['ids']],
            ];

            //诊疗技术
            $chas = Db::name('Characteristic')->where(['status' => 1,'aid' => ['in', $data['ids']], 'userid' => session('c_user.id')])->select();
            $datas[$key]['cha']['total'] = count($chas);
            $datas[$key]['cha']['catname'] = '诊疗技术';
            $datas[$key]['cha']['url'] = url('/client').'#/client/characteristic/index?spm=m-288-282&aid='.$data['area_id'];

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

    /**
     * 显示地区列表
     */
    public function drug() {
        $this->title = '药品统计';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _drug_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $vo['ids']],
            ];

            //药品
            $drugs = Db::name('Drug')->where(['status' => 1,'aid' => ['in', $vo['ids']], 'userid' => session('c_user.id')])->select();
            $vo['drug']['catname'] = '药品';
            $vo['drug']['total'] = count($drugs);
            $vo['drug']['url'] = url('/client').'#/client/drug/index?spm=m-289-284';

            // halt($clients);
        }
    }

    public function _ajax_get_drug()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $data['ids']],
            ];

            //药品
            $drugs = Db::name('Drug')->where(['status' => 1,'aid' => ['in', $data['ids']], 'userid' => session('c_user.id')])->select();
            $datas[$key]['drug']['catname'] = '药品';
            $datas[$key]['drug']['total'] = count($drugs);
            $datas[$key]['drug']['url'] = url('/client').'#/client/drug/index?spm=m-289-284&aid='.$data['area_id'];

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

    /**
     * 显示地区列表
     */
    public function article() {
        $this->title = '资讯统计';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _article_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);

            //新闻动态
            foreach ($this->categorys2 as $k => $v) {
                // halt($vo['ids']);
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $vo['ids']],
                    'userid' => session('c_user.id'),
                ];
                $vo['news'][$v['id']]['catname'] = $v['catname'];
                $vo['news'][$v['id']]['total'] = Db::name($v['module'])->where($map)->count();
                $vo['news'][$v['id']]['url'] = url('/client')."#/client/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-290-230';
            }

            // halt($clients);
        }
    }

    public function _ajax_get_article()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);

            //新闻动态
            foreach ($this->categorys2 as $k => $v) {
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $data['ids']],
                    'userid' => session('c_user.id'),
                ];
                $datas[$key]['news'][$k]['catname'] = $v['catname'];
                $datas[$key]['news'][$k]['total'] = Db::name($v['module'])->where($map)->count();
                $datas[$key]['news'][$k]['url'] = url('/client')."#/client/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-290-230';
            }

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

    /**
     * 显示地区列表
     */
    public function course() {
        $this->title = '课件统计';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _course_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);

            //课件
            foreach ($this->categorys1 as $k => $v) {
                // halt($vo['ids']);
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $vo['ids']],
                    'userid' => session('c_user.id'),
                ];
                $vo['cate'][$v['id']]['catname'] = $v['catname'];
                $vo['cate'][$v['id']]['total'] = Db::name($v['module'])->where($map)->count();
                $vo['cate'][$v['id']]['url'] = url('/client')."#/client/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-290-230';
            }

            // halt($clients);
        }
    }

    public function _ajax_get_course()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);

            //课件
            foreach ($this->categorys1 as $k => $v) {
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $data['ids']],
                    'userid' => session('c_user.id'),
                ];
                $datas[$key]['cate'][$k]['catname'] = $v['catname'];
                $datas[$key]['cate'][$k]['total'] = Db::name($v['module'])->where($map)->count();
                $datas[$key]['cate'][$k]['url'] = url('/client')."#/client/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-290-230';
            }

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

}
